package cn.iocoder.yudao.module.engine.service.core.aichat.client.impl;

import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.module.engine.model.context.AIMessageContext;
import cn.iocoder.yudao.module.engine.model.aichat.AIChatReplayMessage;
import cn.iocoder.yudao.module.engine.service.core.aichat.client.ILLMChatClient;
import lombok.extern.slf4j.Slf4j;
import org.telegram.telegrambots.meta.api.methods.send.SendVoice;
import org.telegram.telegrambots.meta.api.objects.InputFile;
import org.telegram.telegrambots.meta.api.objects.Update;
import org.telegram.telegrambots.meta.api.objects.User;
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
import org.telegram.telegrambots.meta.generics.TelegramClient;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

@Slf4j
public class BotILLMChatClient implements ILLMChatClient {
    private final TelegramClient telegramClient;
    private final Update update;

    public BotILLMChatClient(TelegramClient telegramClient, Update update) {
        this.telegramClient = telegramClient;
        this.update = update;
    }

    @Override
    public void complete(AIMessageContext messageCtx) {
//        final Long chatId = update.getMessage().getChatId();
//        final Integer messageId = update.getMessage().getMessageId();
//        final String fullTextFromAI = textCtx.getFullTextFromAI();
//        final User from = update.getMessage().getFrom();
//
////        EditMessageText replyMessage = EditMessageText.builder()
////                .chatId(chatId)
////                .text(fullTextFromAI)
////                .build();
//        SendMessage replyMessage = SendMessage.builder()
//                .chatId(chatId)
//                .text(fullTextFromAI)
//                .build();
//        try {
//            telegramClient.execute(replyMessage);
//            log.info("发送ChatBoot文本消息成功, userId = {}, chatId = {}, messageId = {}, fullTextFromAI = {}",
//                    from.getId(), chatId, messageId, fullTextFromAI);
//        } catch (TelegramApiException e) {
//            throw new RuntimeException(e);
//        }
    }

    @Override
    public void completeWithError(AIMessageContext messageCtx, Throwable ex) {

    }

    @Override
    public void send(AIMessageContext messageCtx, AIChatReplayMessage res) throws IOException {
        if (res.getAudioData() != null && StrUtil.isNotEmpty(res.getAudioData().getUrl())) {
            File tempFile = null;
            try {
                final Long chatId = update.getMessage().getChatId();
                final Integer messageId = update.getMessage().getMessageId();
                final User from = update.getMessage().getFrom();

                // 下载文件并读取为InputStream
                //tempFile = File.createTempFile(SecureUtil.md5(res.getAudioUrl()), ".mp3");
                //HttpUtil.downloadFileFromUrl(res.getAudioUrl(), tempFile);
                SendVoice replyVoice = SendVoice.builder()
                        .chatId(update.getMessage().getChatId())
                        .voice(new InputFile(new ByteArrayInputStream(res.getAudioData().getAudioBytes()), res.getAudioData().getSentence()))
                        .build();

                telegramClient.execute(replyVoice);
                log.info("发送ChatBoot音频消息成功, userId = {}, chatId = {}, messageId = {}, audioUrl = {}",
                        from.getId(), chatId, messageId, res.getAudioData().getUrl());
            } catch (TelegramApiException e) {
                throw new RuntimeException(e);
            } finally {
                if (tempFile != null && tempFile.exists()) {
                    tempFile.delete();
                }
            }
        }
    }

}
